package net.kucoe.elvn;

import java.io.IOException;

import net.kucoe.elvn.control.TaskAdapter;
import net.kucoe.elvn.util.Config;
import net.kucoe.elvn.util.JsonException;
import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.*;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.*;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;

/**
 * Elvn app
 * 
 * @author Vitaliy Basyuk
 */
public class elvn extends Activity {
    
    private TabHost tabHost;
    private static final String[] menus = { "Plan", "Complete", "Run", "Delete" };
    
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        tabHost = setupTabHost();
        tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
        
        try {
            Config config = new Config() {
                protected String getUserDir() {
                    return Environment.getExternalStorageDirectory().getAbsolutePath();
                }
            };
            setupTab(ListColor.White, config);
            setupTab(ListColor.Teal, config);
            setupTab(ListColor.Stroke, config);
        } catch (IOException e) {
            Log.e("elvn", "error", e);
        } catch (JsonException e) {
            Log.e("elvn", "error", e);
        }
    }
    
    private void setupTab(final ListColor color, final Config config) throws IOException, JsonException {
        List list = config.getList(color);
        String label = list.getLabel();
        View tabview = createTabView(label);
        
        TabSpec spec = tabHost.newTabSpec(label);
        spec.setIndicator(tabview);
        spec.setContent(new TabContentFactory() {
            public View createTabContent(final String tag) {
                try {
                    return createListView(color, config);
                } catch (IOException e) {
                    Log.e("elvn", "error", e);
                } catch (JsonException e) {
                    Log.e("elvn", "error", e);
                }
                TextView textView = new TextView(null);
                textView.setText("Errors");
                return textView;
            }
        });
        tabHost.addTab(spec);
        
    }
    
    private View createTabView(final String label) {
        View view = LayoutInflater.from(tabHost.getContext()).inflate(R.layout.tabs_bg, null);
        TextView tv = (TextView) view.findViewById(R.id.tabsText);
        tv.setText(label);
        return view;
    }
    
    protected ListView createListView(final ListColor color, final Config config) throws IOException, JsonException {
        ListView listView = (ListView) findViewById(R.id.list_view);
        java.util.List<Task> tasks = config.getList(color).getTasks();
        TypedArray buttons = getResources().obtainTypedArray(R.array.buttons);
        TaskAdapter adapter = new TaskAdapter(getApplicationContext(), R.layout.task, tasks, buttons);
        listView.setAdapter(adapter);
        registerForContextMenu(listView);
        return listView;
    }
    
    private TabHost setupTabHost() {
        TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();
        return tabHost;
    }
    
    @Override
    public void onCreateContextMenu(final ContextMenu menu, final View view, final ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, view, menuInfo);
        if (view instanceof ListView) {
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
            String text = (String) ((ListView) view).getAdapter().getItem(info.position);
            
            menu.setHeaderTitle("Options for " + text);
            for (int i = 0; i < menus.length; i++) {
                menu.add(Menu.NONE, i, i, menus[i]);
            }
        }
    }
    
    @Override
    public boolean onContextItemSelected(final MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int menuItemIndex = item.getItemId();
        String menuItemName = menus[menuItemIndex];
        Log.d("elvn", String.format("Selected %s for item %d", menuItemName, info.position));
        return true;
    }
    
}
